package com.hgb.mall.portal.services.impl;

import com.hgb.mall.mbg.entity.SmsSearchHistory;
import com.hgb.mall.mbg.mapper.SmsSearchHistoryDynamicSqlSupport;
import com.hgb.mall.mbg.mapper.SmsSearchHistoryMapper;
import com.hgb.mall.portal.services.SearchHistoryService;
import com.hgb.mall.portal.services.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;

/**
 * @Author: guibo
 * @Description:
 * @Date: Created in 17:00 2021/5/21
 * @Modified By:
 */
@Service
public class SearchHistoryServiceImpl implements SearchHistoryService {
    @Autowired
    SmsSearchHistoryMapper searchHistoryMapper;
    @Autowired
    UmsMemberService memberService;
    @Override
    public Integer saveSearchHistory(String keyword) {
        searchHistoryMapper.delete(c->c
                .where(SmsSearchHistoryDynamicSqlSupport.keyword,isEqualTo(keyword)));
        SmsSearchHistory searchHistory = new SmsSearchHistory();
        searchHistory.setKeyword(keyword);
        searchHistory.setMenberId(memberService.getCurrentMember().getId());
        searchHistory.setDeleteStatus(0);
        searchHistory.setCreateTime(new Date());
        return searchHistoryMapper.insert(searchHistory);
    }

    @Override
    public List<SmsSearchHistory> list() {
        return searchHistoryMapper.select(c->c
                .where(SmsSearchHistoryDynamicSqlSupport.menberId,
                        isEqualTo(memberService.getCurrentMember().getId()))
                .orderBy(SmsSearchHistoryDynamicSqlSupport.menberId.descending())
        );
    }

    @Override
    public Integer deleteAll() {
        return searchHistoryMapper.delete(c->c
                .where(SmsSearchHistoryDynamicSqlSupport.menberId,
                        isEqualTo(memberService.getCurrentMember().getId()))
        );
    }
}
